package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.IdcardInfo;
import com.sc.nft.entity.vo.IdcardInfoVO;
import com.sc.nft.entity.vo.UserTelPageVO;
import com.sc.nft.enums.IdCardIsPassTypeEnum;
import com.sc.nft.mapper.IdcardInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * (sc_idcard_info)数据DAO
 *
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2022-06-01 18:12:41
 */
@Slf4j
@Repository
public class IdcardInfoDao extends ServiceImpl<IdcardInfoMapper, IdcardInfo> {

    @Resource
    private IdcardInfoMapper idcardInfoMapper;

    @Resource
    private StaticDataDao staticDataDao;

    public List<IdcardInfo> listIdCardByRegion(String region) {
        LambdaQueryWrapper<IdcardInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.likeRight(IdcardInfo::getCardId, region);
        return list(queryWrapper);
    }

    public List<IdcardInfo> findListByUserIdAndIsPass(Long userId, Integer isPass) {
        LambdaQueryWrapper<IdcardInfo> idcardInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        idcardInfoLambdaQueryWrapper.eq(IdcardInfo::getUserId, userId);
        idcardInfoLambdaQueryWrapper.eq(IdcardInfo::getIsPass, isPass);
        return list(idcardInfoLambdaQueryWrapper);
    }

    public List<IdcardInfo> findListByCardIdAndIsPass(String cardId, Integer isPass) {
        LambdaQueryWrapper<IdcardInfo> idcardLambdaQueryWrapper = new LambdaQueryWrapper<>();
        idcardLambdaQueryWrapper.eq(IdcardInfo::getCardId, cardId);
        idcardLambdaQueryWrapper.eq(IdcardInfo::getIsPass, isPass);
        return list(idcardLambdaQueryWrapper);
    }

    public List<IdcardInfo> findByCardId(String cardId) {
        LambdaQueryWrapper<IdcardInfo> idcardLambdaQueryWrapper = new LambdaQueryWrapper<>();
        idcardLambdaQueryWrapper.eq(IdcardInfo::getCardId, cardId);
        return list(idcardLambdaQueryWrapper);
    }


    public IdcardInfo findLastOneByUserId(Long userId) {
        return idcardInfoMapper.findLastOneByUserId(userId);
    }

    public Page<IdcardInfoVO> pageIdCard(Integer pageNo, Integer pageSize, String name, String userTel, String userEmail) {
        String imgHost = staticDataDao.getByType("imgHost").getValue();

        return idcardInfoMapper.pageIdCard(new Page(pageNo, pageSize), name, userTel, userEmail, imgHost);
    }

    public List<IdcardInfo> getListByIsPass() {
        LambdaQueryWrapper<IdcardInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IdcardInfo::getIsPass, IdCardIsPassTypeEnum.UNDER_REVIEW.getCode());
        return list(wrapper);
    }

    public List<IdcardInfo> findListByIsPassAndIsClose() {
        LambdaQueryWrapper<IdcardInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IdcardInfo::getIsPass, IdCardIsPassTypeEnum.SUCCESS.getCode());
        wrapper.eq(IdcardInfo::getIsClose, Boolean.FALSE);
        return list(wrapper);
    }

    public IdcardInfo getByUserIdAndIsPass(Long userId) {
        LambdaQueryWrapper<IdcardInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IdcardInfo::getUserId, userId);
        wrapper.in(IdcardInfo::getIsPass, IdCardIsPassTypeEnum.SUCCESS.getCode(), IdCardIsPassTypeEnum.UNDER_REVIEW.getCode());
        wrapper.last("limit 1");

        return getOne(wrapper);
    }

    public List<IdcardInfo> getListByXX() {
        LambdaQueryWrapper<IdcardInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IdcardInfo::getIsPass, IdCardIsPassTypeEnum.SUCCESS.getCode());
        wrapper.last("order by id desc limit 500");
        return list(wrapper);
    }

    public IdcardInfo findLastOneByUserIdAndIsPass(Long userId) {
        LambdaQueryWrapper<IdcardInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IdcardInfo::getIsPass, IdCardIsPassTypeEnum.SUCCESS.getCode());
        wrapper.eq(IdcardInfo::getUserId, userId);
        wrapper.last("order by id desc limit 1");
        return getOne(wrapper);
    }

    public void deleteByUserId(Long userId) {
        LambdaQueryWrapper<IdcardInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IdcardInfo::getUserId, userId);
        remove(wrapper);
    }

    public Boolean userRealName(Long userId) {
        LambdaUpdateWrapper<IdcardInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(IdcardInfo::getIsPass, IdCardIsPassTypeEnum.SUCCESS.getCode());
        wrapper.eq(IdcardInfo::getUserId, userId);
        wrapper.eq(IdcardInfo::getIsPass, IdCardIsPassTypeEnum.UNDER_REVIEW.getCode());
        return update(wrapper);
    }

    /**
     * 获取用户账号信息分页列表（用于修改手机号展示）
     *
     * @param page
     * @param userTel
     * @return
     */
    public IPage<UserTelPageVO> userTelRecordPage(Page page, String userTel) {
        return baseMapper.userTelRecordPage(page, userTel);
    }
}